package cn.tedu.tmall.admin.content.service.impl;


import cn.tedu.tmall.admin.content.dao.repository.IArticleRepository;
import cn.tedu.tmall.admin.content.dao.repository.ICategoryRepository;
import cn.tedu.tmall.admin.content.pojo.param.ArticleAddParam;
import cn.tedu.tmall.admin.content.pojo.po.ArticleDetailPO;
import cn.tedu.tmall.admin.content.pojo.po.ArticlePO;
import cn.tedu.tmall.admin.content.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.content.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**要实现service和mapper分离
 *
 */
@Service
@Slf4j
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private IArticleRepository articleRepository;
    @Autowired
    private ICategoryRepository categoryRepository;

    /**
     * 业务流程
     * 1 文本标题是否能重复
     *  普通用户  可以重复
     *  管理员    发布公告,不能重复
     * 2  文章类别是否存在
     *    2.1  需要传类别id
     *    2.2  判断该id分类是否存在,不存在报错
     * 3  文章类别是否可以添加 enable
     * 4 保存文章 失败异常
     * @param articleAddParam
     */
    @Override
    @Transactional
    public void add(ArticleAddParam articleAddParam) {
        /**
         *增加文章
         *
         */
        //1.文本标题是否能重复
        String title = articleAddParam.getTitle();
        log.debug("增加文章-入参:{}",title);
        Integer count = articleRepository.countArticleByTitle(title);
        if(count==0) {
            //2. 判断类别是否存在
            Long categoryId = articleAddParam.getCategoryId();

            CategoryPO categoryPO = categoryRepository.getCategoryById(categoryId);
            if(categoryPO==null) {
                throw new RuntimeException("类别不存在,发布文章失败");
            }else {
                Integer enable = categoryPO.getEnable();
                if(enable==1) {
                    //类别启用
                    //需要开启事务    同成同败
                    //1.添加文章
                    ArticlePO articlePO = new ArticlePO();
                    BeanUtils.copyProperties(articleAddParam,articlePO);
                    Integer data = articleRepository.saveArticle(articlePO);
                    if(data!=1) {
                        throw new RuntimeException("添加文章表失败!!");
                    }
                        //doSome();
                    //2.添加文章详细
                    ArticleDetailPO articleDetailPO = new ArticleDetailPO();
                    BeanUtils.copyProperties(articleAddParam,articleDetailPO);
                    articleDetailPO.setArticleId(articlePO.getId());
                    Integer articleCount = articleRepository.saveArticleDetail(articleDetailPO);
                    if(articleCount !=1) {
                        throw new RuntimeException("添加文章详情表失败!!");
                    }
                }else {
                    throw new RuntimeException("类别未启用,文章发布失败");
                }
            }

        }else {
            throw new RuntimeException("标题已存在,文章发布失败!!!");
        }
    }
    private void doSome(){
        throw new RuntimeException("数据库异常");
    }

}
